#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>> threeSum;
        sort(nums.begin(), nums.end());
        int c = nums.size() - 1;
        while (c)
        {
            int left = 0;
            int right = c - 1;
            while (left < right)
            {
                int target = -nums[c];
                if (nums[left] + nums[right] > target)
                {
                    right--;
                }
                else if (nums[left] + nums[right] < target)
                {
                    left++;
                }
                else
                {

                    threeSum.push_back({ nums[left],nums[right],nums[c] });
                    left++;
                    right--;
                    while (left < right && nums[left] == nums[left - 1])
                    {
                        left++;
                    }
                    while (left < right && nums[right] == nums[right + 1])
                    {
                        right--;
                    }
                }

            }
            c--;
            while (c > 0 && nums[c] == nums[c + 1])c--;
        }
        return threeSum;
    }
};